package com.mallcai.bigdata.ladon.service.dashboard.impl;

import com.mallcai.bigdata.ladon.dpl.entity.dashboard.SqlRunLogStatic;
import com.mallcai.bigdata.ladon.dpl.mapper.dashboard.SqlRunLogStaticMapper;
import com.mallcai.bigdata.ladon.service.dashboard.ISqlRunLogStaticService;
import com.mallcai.bigdata.uid.IUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SqlRunLogStaticService implements ISqlRunLogStaticService {

    @Autowired
    private SqlRunLogStaticMapper sqlRunLogStaticMapper;
    @Autowired
    private IUidGenerator iUidGenerator;

    //按照时间和报表获取请求次数的统计数据
    @Override
    public List<SqlRunLogStatic> getCountData(Map<String, Object> params) {
        List<SqlRunLogStatic> basicStatic = new ArrayList<>();
        //如果有全部的选项的话，按照时间对当天的全部请求次数进行加和，返回统计数据
        if (judueIfSelectAll(params)) {
            List<SqlRunLogStatic> allCountData = sqlRunLogStaticMapper.getAllReportCountDataByDate(params);
            basicStatic.addAll(allCountData);
        }
        basicStatic.addAll(sqlRunLogStaticMapper.getByParams(params));
        return basicStatic;
    }

    //按照时间和报表获取平均请求时间的统计数据
    @Override
    public List<SqlRunLogStatic> getAvgTimeData(Map<String, Object> params) {
        List<SqlRunLogStatic> basicStatic = new ArrayList<>();
        //如果有全部的选项的话，按照时间对当天的全部请求进行平均时间统计，返回统计数据
        if (judueIfSelectAll(params)) {
            List<SqlRunLogStatic> allCountData = sqlRunLogStaticMapper.getAllReportAvgTimeDataByDate(params);
            basicStatic.addAll(allCountData);
        }
        //这里的顺序还是挺重要的，如果有全部就先统计全部的然后再查每个报表每天的数据，保证时间有序前端就不用排序了
        basicStatic.addAll(sqlRunLogStaticMapper.getByParams(params));
        return basicStatic;
    }

    //按照报表获取十个最大请求次数的报表
    @Override
    public List<SqlRunLogStatic> getTopCount(Map<String, Object> params) {
        return sqlRunLogStaticMapper.getTopCount(params);
    }

    //按照报表获取十个最大平均请求时间的报表
    @Override
    public List<SqlRunLogStatic> getTopAvgTime(Map<String, Object> params) {
        return sqlRunLogStaticMapper.getTopAvgTime(params);
    }

    @Override
    public int mergeInsert(List<SqlRunLogStatic> sqlRunLogStatics) {
        for (SqlRunLogStatic sqlRunLogStatic : sqlRunLogStatics) {
            sqlRunLogStatic.setId(iUidGenerator.getUid());
        }
        int fromIndex = 0;
        while (fromIndex < sqlRunLogStatics.size()) {
            List subList = sqlRunLogStatics.subList(fromIndex, Math.min(fromIndex + 50, sqlRunLogStatics.size()));
            Map<String, Object> map = new HashMap<>();
            map.put("list", subList);
            sqlRunLogStaticMapper.mergeInsert(map);
            fromIndex += 50;
        }
        return 0;
    }

    private boolean judueIfSelectAll(Map<String, Object> params) {
        if (params != null && params.containsKey("codes")) {
            List<String> codes = (List<String>) params.get("codes");

            return codes.indexOf("全部") > -1;
        }
        return false;
    }

    /**
     * 总访问次数
     *
     * @return int
     */
    @Override
    public int totalVisitSize() {
        return sqlRunLogStaticMapper.getVisitSize();
    }
}
